﻿/*
 * LegendChildNodes
 * 
 * initial implementation : 20.10.2008
 *
 * Copyright (c) Adam Sida, asida@centrum.cz.
 * All rights reserved.
 * 
 * for license see Petra.Gis.License.txt
 */
using System;
using System.Collections;
using System.Collections.Generic;
using Petra.Gis.RenderingServices.PlanarBase.Support;


namespace Petra.Gis.RenderingServices.PlanarBase
{


  /// <summary>
  /// represents enumerable List of legend nodes
  /// <para>
  /// this class must inherit from IList<T> not from List<T>
  /// reason is that add, remove, insert methods can't be overriden in System.Collections.GenericList<T>.
  /// this is workaround.
  /// </para>
  /// </summary>
  //public class LegendChildNodes : KeepRelationWith<object>, IChildItemsList<LegendNodeBase>
  public class LegendChildNodes : ChildItemsList<LegendNodeBase>
  {


    private List<LegendNodeBase> _items;


    internal LegendChildNodes(LeafLegendNode parent)
      :base()
    {
      ParentNode = parent;
    }




    /// <summary>
    /// where this list is initialized
    /// </summary>
    protected IList<RenderableLegendNodeBase> writableDisplayOrder
    { get { return (IList<RenderableLegendNodeBase>)__getPropetyValue(this.ParentNode.Legend, "writableDisplayOrder"); } }




    public LeafLegendNode ParentNode
    {
      get { return (LeafLegendNode)__parent; }
      protected set
      {

        //if (_nodeType != LegendNodeType.GroupNode)
        //  throw new InvalidOperationException("cant append into non grou type node");

        //null is disabled for LegendNodeBase
        if (value == null)
          throw new InvalidOperationException(@"error unsupported null as parent
                                                use RemoveNode() instead");

        //if parent changed....
        if (__parent != null && !__parent.Equals(value))
          throw new NotImplementedException("sorry not avail yet");

        __parent = value;
      }
    }





    /// <summary>
    /// Adds an item to the end of item list
    /// </summary>
    /// <param name="item">The item to be added to the end of the list</param>
    public override void Add(LegendNodeBase newChild)
    {
      if (IsReadOnly)
        throw new InvalidOperationException();

      if (newChild.ParentNode != null && (newChild.ParentNode != ParentNode))
        if (!newChild.ParentNode.Legend.Equals(ParentNode.Legend))
          throw new NotImplementedException("sorry not avail now");

      if (newChild.ParentNode != null && (newChild.ParentNode != ParentNode))
        // reinsert existing node into another place
        // display order remains same
        newChild.ParentNode.ChildNodes.Remove(newChild);

      else
        // new node insert, put also into displayOrder collection
        if (newChild is RenderableLegendNodeBase)
          ParentNode.Legend.ChildNodes.writableDisplayOrder.Insert(0, (RenderableLegendNodeBase)newChild);

      __setParent(this.ParentNode, newChild);
      base.Add(newChild);
    }




    /// <summary>
    /// indicates whether list is readonly.
    /// </summary>
    public override bool IsReadOnly
    {
      get { return false; }
    }



  }

}
